﻿using System;
using System.Web;
using System.IO;

namespace Common.Log
{
    public class LogHelper
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger("Logging");
        public static void Debug(string message)
        {
            if (log.IsDebugEnabled)
            {
                log.Debug(message);
            }
        }
        public static void Debug(System.Exception ex1)
        {
            if (log.IsDebugEnabled)
            {
                log.Debug(ex1.Message.ToString() + "/r/n" + ex1.Source.ToString() + "/r/n" + ex1.TargetSite.ToString() + "/r/n" + ex1.StackTrace.ToString());
            }
        }
        public static void Error(string message)
        {
            if (log.IsErrorEnabled)
            {
                log.Error(message);
            }
        }
        public static void Fatal(string message)
        {

            if (log.IsFatalEnabled)
            {
                log.Fatal(message);
            }
        }
        public static void Info(LogInfoModel logInfoModel)
        {
            if (log.IsInfoEnabled)
            {
                StreamWriter sw = ChangeLog4netLogFileName(log, logInfoModel.OperatorID.Substring(0, 6));
                log.Info(logInfoModel);
                if (sw != null)
                {
                    sw.Flush();
                    sw.Close();
                }
            }
        }

        public static void Warn(string message)
        {
            if (log.IsWarnEnabled)
            {
                log.Warn(message);
            }
        }

        /// <summary>
        /// 动态生成日志文件
        /// </summary>
        /// <param name="iLog"></param>
        /// <param name="bossId"></param>
        private static StreamWriter ChangeLog4netLogFileName(log4net.ILog iLog, string bossId)
        {
            StreamWriter sw = null;
            log4net.Core.LogImpl logImpl = iLog as log4net.Core.LogImpl;
            if (logImpl != null)
            {
                log4net.Appender.AppenderCollection ac = ((log4net.Repository.Hierarchy.Logger)logImpl.Logger).Appenders;
                for (int i = 0; i < ac.Count; i++)
                {
                    log4net.Appender.FileAppender rfa = ac[i] as log4net.Appender.FileAppender;
                    if (rfa != null)
                    {
                        string md5 = Common.SecurityHelper.Safe.MD5(bossId);
                        string path = "/Log/" + md5.Substring(0, 1) + "/" + md5.Substring(1, 1) + "/" + md5.Substring(2, 1) + "/" + md5.Substring(3, 1) + "/";
                        string filePath = HttpContext.Current.Server.MapPath(path);
                        string strFilename = filePath + DateTime.Now.ToString("yyyyMMdd") + ".txt";

                        if (!System.IO.Directory.Exists(filePath))
                        {
                            System.IO.Directory.CreateDirectory(filePath);
                        }

                        rfa.File = strFilename;

                        //rfa.Writer = new System.IO.StreamWriter(rfa.File, rfa.AppendToFile, rfa.Encoding);

                        sw = new StreamWriter(rfa.File, rfa.AppendToFile, rfa.Encoding);
                        rfa.Writer = sw;
                    }
                }
            }
            return sw;
        }
    }
}
